﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using Microsoft.Phone.Net.NetworkInformation;
using MSPToolkit.Encodings;

namespace VB
{
    public class LoadManager
    {
        public event Action<string> OnLoad;
        public event Action<DownloadProgressChangedEventArgs> OnProgress;
        public event Action<Exception> OnError;
        public event Action OnCancel;
        public event Action OnStart;
        public event Action OnFinish;
        public event Action OnNoNetwork;
        public event Action<string> OnSaveTo;
        public string SaveTo;
        public int Encoding;

        public void Load(string url)
        {
            if (OnStart != null)
                OnStart();
            try
            {
                if (!NetworkInterface.GetIsNetworkAvailable())
                {
                    if (OnNoNetwork != null)
                    {
                        OnNoNetwork();
                        if (OnFinish != null)
                            OnFinish();
                    }
                    else
                        throw new Exception("No network available.");
                }
                else
                {
                    WebClient webClient = new WebClient();
                    if (Encoding > 0)
                        webClient.Encoding = BaseSingleByteEncoding.GetEncoding(Encoding);
                    webClient.DownloadStringAsync(new Uri(url));
                    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCompleted);
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                }
            }
            catch (Exception ex)
            {
                if (OnFinish != null)
                    OnFinish();
                MessageBox.Show(ex.Message, "LoadManager Init error", MessageBoxButton.OK);
            }
        }

        void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (OnProgress != null)
                OnProgress(e);
        }

        private void webClient_DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled)
                {
                    if (OnCancel != null)
                        OnCancel();
                }
                else if (e.Error != null)
                {
                    if (OnError != null)
                        OnError(e.Error);
                    else
                        throw new Exception(e.Error.Message);
                }
                else
                {
                    if (SaveTo != null)
                    {
                        if (OnSaveTo != null)
                            OnSaveTo(e.Result);
                        else
                        {
                            FileManager File = new FileManager();
                            File.Save(SaveTo, e.Result);
                        }
                    }
                    if (OnLoad != null)
                        OnLoad(e.Result);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "LoadManager Download error", MessageBoxButton.OK);
            }
            if (OnFinish != null)
                OnFinish();
        }

    }
}
